<template>
  <div class="main">
    <div class="mainMain">
      <div class="searchForm">
        <el-form :inline="true" :model="params" style="margin-top: 20px">
          <el-form-item label="标题">
            <el-input
              v-model="params.title"
              placeholder="按标题查找"
              class="block"
            >
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" @click="getList(1)" class="block"
              >查询
            </el-button>
            <el-button type="primary" icon="el-icon-plus" @click="add"
              >增加
            </el-button>
            <el-button type="danger" icon="el-icon-delete" @click="removeMore"
              >批量删除
            </el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="message">
        <el-table :data="rows" style="width: 100%" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column prop="name" label="品牌名称"> </el-table-column>
          <el-table-column prop="img" label="图片">
            <template slot-scope="scope">
              <img style="width:40px;height:40px" v-if="scope.row.img" :src="scope.row.img" />
            </template>
          </el-table-column>
          <el-table-column prop="sort" label="排序"> </el-table-column>
          <el-table-column prop="status" label="是否启用">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.status"
                active-color="#13ce66"
                inactive-color="#ff4949"
                @change="statusChange(scope.row)"
                :active-value="1"
                :inactive-value="0"
              >
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="320">
            <template slot-scope="scope">
              <el-button
                type="primary"
                size="mini"
                icon="el-icon-edit"
                @click="edit(scope.row)"
                >编辑</el-button
              >

              <el-popconfirm
                title="确定删除词数据吗？"
                @confirm="remove(scope.row)"
              >
                <el-button
                  type="danger"
                  size="mini"
                  icon="el-icon-delete"
                  slot="reference"
                  >删除</el-button
                >
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <div class="bottomLeft">
      <el-pagination
        @size-change="sizeChange"
        @current-change="getList"
        :current-page="params.pageNum"
        :page-sizes="[5, 10, 100, 200, 300, 400]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>

    <BrandEdit @load="getList()" ref="brandEditer"></BrandEdit>

  </div>
</template>

<script>
import BrandEdit from './brandEdit'
export default {
  name: "indexbrand",
  components: {
    //HelloWorld
    BrandEdit
  },
  data() {
    return {
      params: {
        title: "",
        type: "brand",
        pageNum: 1,
        pageSize: 5,
      },
      rows: [],
      total: 0,
      selecteds:[]
    };
  },
  methods: {
    add(){
      this.$refs.brandEditer.add()
    },
    edit(row){
      this.$refs.brandEditer.edit(row)
    },
    getList(pageNum) {
      if (pageNum) this.params.pageNum = pageNum;
      this.$http({
        url: "/admin/index/brand",
        params: this.params,
      }).then((res) => {
        // console.log(banner);
        this.total = res.total;
        this.rows = res.rows;
      });
    },
    ///启用停用
    statusChange(row) {
      console.log(row);
      this.$http({
        url: "/admin/index/brand",
        method: "PUT",
        data: {
          indexId: row._id,
          status: row.status,
        },
      }).then((res) => {
        if (res.errorMsg) {
          this.getList(1);
        }
      });
    },
    sizeChange(pageSize) {
      this.params.pageSize = pageSize;
      this.getList(1);
    },
    ////删除
    remove(row) {
      this.$http({
        url: "/admin/index/brand",
        method: "DELETE",
        params: { indexId: row._id },
      }).then((res) => {
        if (!res.errorMsg) {
          this.getList(1);
        }
      });
    },
    handleSelectionChange(rows){
      // console.log(row)
      ///存储选择数据
      this.selecteds = rows
    },
    removeMore(){
      let checkRowsPromise = []
      let success=[],fail=[]
      for(let i = 0;i<this.selecteds.length; i++){
        checkRowsPromise.push(
          new Promise(resolve => {
            let row = this.selecteds[i]
            this.$http({
              url:'/admin/index/brand',
              method:'DELETE',
              params:{indexId:row._id}
            }).then(res => {
              if(!res.errorMsg){
                success.push(row._id)
              }else{
                fail.push(row._id)
              }
              resolve()
            })
            
          })
        )
      }
      this.$confirm('此操作将删除选中的数据局，是否继续？', '提示', {
        confirmButtonText:'确定',
        cancelButtonText:'取消',
        type:'waring'

      }).then(() =>{
        Promise.all(checkRowsPromise).then(() =>{
        this.$message({
          message:`批量删除完成，删除${success.legnth}条数据，删除失败${fail.length}`,
          duration:3000
        })
        this.getList(1)
      })
      })
      
    },
  },
  created() {
    this.getList();
  },
  mounted() {},
};
</script>

<style>
/* scoped表示样式仅对当前页面生效 */

.main {
  margin-top: 50px;
}
.demonstration {
  margin-top: 15px;
}
.rightMain {
  width: 1091px;
  height: 61px;
  margin-top: 10px;
  margin-left: 40px;
  background: #ffffff;
}
.rightMain p {
  width: 30px;
  height: 18px;
  margin-top: 20px;
  margin-left: 38px;
  font-size: 14px;
  font-family: MicrosoftYaHeiUI-, MicrosoftYaHeiUI;
  color: #7c7c7d;
}
.rightMain input {
  width: 192px;
  height: 40px;
  margin-top: 10px;
  border: 1px solid #ccc;
}
.search {
  width: 71px;
  height: 31px;
  margin-top: 16px;
  margin-left: 30px;
  color: #ffffff;
  text-align: center;
  font-size: 16px;
  line-height: 31px;
  background: #63b0ff;
  border-radius: 10px 10px 10px 10px;
}
.search img {
  margin-left: 10px;
}
.delete {
  width: 71px;
  height: 31px;
  margin-top: 16px;
  margin-left: 30px;
  border-radius: 10px 10px 10px 10px;
}

.mainMain {
  width: 1091px;
  margin-top: 10px;
  margin-left: 40px;
  background: #ffffff;
}
.mainMain table {
  margin-left: 40px;
  margin-top: 20px;
}
.mainMain table td {
  width: 300px;
  height: 50px;
  border-bottom: 1px solid #ccc;
}
.mainBottom {
  display: flex;
}
.bottomLeft {
  display: flex;
  width: 188px;
  height: 41px;
  margin-top: 10px;
  margin-left: 51px;
  background: #ffffff;
}
.totaWord {
}
.total {
  width: 80px;
  height: 20px;
  border: 1px solid #eab924;
}
</style>